<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <el-row class="footer" type="flex" justify="center">
      <el-col>
        <el-button type="primary" size="small" @click="addRole">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserPhoto } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    assignRoleid: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []
    }
  },
  created() {
    this.getRoles()
    this.getUserInfo()
  },
  methods: {
    // 获取角色
    async getRoles() {
      const res = await getRoles()
      console.log(res)
      this.roleList = res.data.rows
    },
    // 获取角色详情
    async getUserInfo() {
      const res = await getUserPhoto(this.assignRoleid)

      this.roleIds = res.data.roleIds === null ? [] : res.data.roleIds
    },
    // 分配角色
    async addRole() {
      console.log(this.roleIds)
      const res = await assignRoles({ id: this.assignRoleid, roleIds: this.roleIds })

      this.$message(res.message)
      this.$emit('close')
    },
    // 点击取消分配
    cancleDialog() {
      this.$emit('close')
    }
  }

}
</script>

<style lang="scss" scoped>
.footer{
    margin-top: 20px;
    text-align: center;
}
</style>>

